home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Applications / Communication / NewsBase / Source / common.subproj / Localization.m < prev    next >
Text File  |  1993-01-12  |  6KB  |  239 lines

  1. /* You may freely copy, distribute, and reuse the code in this example.
  2.  * NeXT disclaims any warranty of any kind, expressed or  implied, as to its
  3.  * fitness for any particular use.
  4.  * 
  5.  */
  6.  
  7. #import <appkit/Application.h>
  8. #import <appkit/NXImage.h>
  9. #import <appkit/nextstd.h>
  10. #import <objc/NXStringTable.h>
  11. //#import <appkit/defaults.h>
  12. #import <libc.h>
  13. #import "Localization.h"
  14.  
  15. #define NATIVE_LANGUAGE "English"
  16. static char *launchDir = NULL;    /* if this is NULL, it will be calculated */
  17.  
  18. /*
  19.  * launchDirOK will return a NULL launchDir if you launch from a Terminal Shell. 
  20.  */
  21. static BOOL launchDirOk()
  22. {
  23.     const char *slash;
  24.  
  25.     if (launchDir) return YES;
  26.  
  27.     slash = strrchr(NXArgv[0], '/');
  28.     if (slash && slash-NXArgv[0]) {
  29.     launchDir = malloc((slash-NXArgv[0]+1)*sizeof(char));
  30.     strncpy(launchDir, NXArgv[0], slash-NXArgv[0]);
  31.     launchDir[slash-NXArgv[0]] = '\0';
  32.     return YES;
  33.     }
  34.  
  35.     return NO;
  36. }
  37.  
  38. void InitLocalDateAndTime()
  39. {
  40.     time_t t;
  41.     char buffer[16];
  42.     t = time(NULL);
  43.     strftime(buffer, 15, "%H:%M", localtime(&t));
  44. }
  45.  
  46.         
  47. void LocalDateAndTime(char *buffer,int  maxBufferSize, const time_t *time) 
  48. {
  49.     const char *format;
  50.     
  51.     format = NXGetDefaultValue([NXApp appName], "NXDateAndTime");
  52.     if (format == NULL)
  53.         format = "%a %b %d %H:%M:%S %Z %Y";
  54.     strftime(buffer,maxBufferSize,format,localtime(time));
  55. }
  56.  
  57. void LocalDate(char *buffer,int  maxBufferSize, const time_t *time)
  58. {
  59.     const char *format;
  60.     
  61.     format = NXGetDefaultValue([NXApp appName], "NXDate");
  62.     if (format == NULL)
  63.         format = "%a %b %d %Y";
  64.     strftime(buffer,maxBufferSize,format,localtime(time));
  65. }
  66.  
  67. void LocalTime(char *buffer,int maxBufferSize, const time_t *time)
  68. {
  69.     const char *format;
  70.     
  71.     format = NXGetDefaultValue([NXApp appName], "NXTime");
  72.     if (format == NULL)
  73.         format = "%H:%M:%S %Z";
  74.     strftime(buffer,maxBufferSize,format,localtime(time));
  75. }
  76.  
  77.  
  78. /* If the user has not set a language preference, load the nib
  79.  * section stored in the Mach-O by default. Otherwise,
  80.  * the nib file loaded would follow the language set in the Preferences
  81.  * Application.
  82.  */
  83. id LoadLocalNib(const char *nibFile, id owner, BOOL withName, NXZone *zone)
  84. {
  85.     BOOL found = NO;
  86.     id retval = nil;
  87.     const char *const *languages;
  88.     char path[MAXPATHLEN+1];
  89.  
  90.     languages = [NXApp systemLanguages];
  91.  
  92.     if (languages && launchDirOk()) {
  93.     while (!found && *languages) {
  94.         //if (!strcmp(*languages, NATIVE_LANGUAGE))      break;
  95.         sprintf(path, "%s/%s.lproj/%s", launchDir, *languages, nibFile);
  96.         if (!access(path, R_OK)) {
  97.         if ( zone == NULL){ 
  98.             retval = [NXApp loadNibFile:path owner:owner 
  99.                 withNames:NO];
  100.             found = YES;
  101.         }
  102.         else {
  103.             retval = [NXApp loadNibFile:path owner:owner 
  104.                 withNames:withName fromZone:zone];
  105.             found = YES;
  106.         }
  107.         }
  108.     languages++;
  109.     }
  110.     }
  111.  
  112.     if ( zone == NULL) 
  113.        return found ? retval : [NXApp loadNibSection:nibFile owner:owner
  114.                 withNames:NO];
  115.     else
  116.        return found ? retval : [NXApp loadNibSection:nibFile owner:owner
  117.                 withNames:withName fromZone:zone];
  118. }
  119.  
  120. NXStream *LocalMapFile(const char *file, int flag)
  121. {
  122.     BOOL found = NO;
  123.     const char *const *languages;
  124.     char path[MAXPATHLEN+1];
  125.     NXStream    *stream=NULL;
  126.  
  127.     languages = [NXApp systemLanguages];
  128.  
  129.     if (languages && launchDirOk()) {
  130.     while (!found && *languages) {
  131.         sprintf(path, "%s/%s.lproj/%s", launchDir, *languages, file);
  132.         if (!access(path, R_OK)) {
  133.         stream = NXMapFile(path, flag);
  134.         found = YES;
  135.         }
  136.     languages++;
  137.     }
  138.     }
  139.     if (!found) {
  140.     sprintf(path, "%s/%s", launchDir, file);
  141.     stream = NXMapFile(path, flag);
  142.     }
  143.     return (NXStream *)stream;
  144. }
  145.  
  146.  
  147.  
  148. id LocalImage(const char *file)
  149. {
  150.     id retval = nil;
  151.     const char *const *languages;
  152.     char path[MAXPATHLEN+1];
  153.  
  154.     languages = [NXApp systemLanguages];
  155.     if (languages) {
  156.     while (!retval && *languages) {
  157.         if (!strcmp(*languages, NATIVE_LANGUAGE)) break;
  158.         sprintf(path, "%s/%s.lproj/%s", launchDir, *languages, file);
  159.         retval = [NXImage newFromFile:file];
  160.         languages++;
  161.     }
  162.     } else {
  163.     retval = [NXImage newFromSection:file];
  164.     }
  165.  
  166.     return retval;
  167. }
  168.  
  169. /* If the user has not selected a language preference,
  170.  * return the key by default. 
  171.  * NOTE: The key is in the same language as the NATIVE_LANGUAGE.
  172.  */
  173.  
  174. const char *doLocalString(const char *tableName, const char *key, const char *value)
  175. {
  176.     id table = nil;
  177.     const char *tableValue;
  178.     const char *const *languages;
  179.     BOOL isMyAppsLanguage = NO;
  180.     char buffer[MAXPATHLEN+1];
  181.     static int debugLanguages = -1;
  182.     static id stringTableTable = nil;
  183.  
  184.     if (!(languages = [NXApp systemLanguages])) return value ? value : key;
  185.  
  186.     if (!tableName) tableName = [NXApp appName];
  187.     if (tableName[0] == '"' && tableName[strlen(tableName)-1] == '"') {
  188.     strcpy(buffer, tableName+1);
  189.     buffer[strlen(buffer)-1] = '\0';
  190.     tableName = NXUniqueString(buffer);
  191.     }
  192.  
  193.     if (debugLanguages < 0) debugLanguages = NXGetDefaultValue([NXApp appName], "NXDebugLanguages") ? 1 : 0;
  194.  
  195.     if (!(table = [stringTableTable valueForKey:tableName])) {
  196.     while (!table && *languages) {
  197.         if (!strcmp(*languages, NATIVE_LANGUAGE)) {
  198.         isMyAppsLanguage = YES;
  199. /*
  200.         sprintf(buffer, "%s/%s.lproj/%s.strings", launchDir, *languages, tableName);
  201.                 table =  [[NXStringTable alloc] init] ;
  202.         [table readFromFile:buffer];
  203.         
  204. */
  205.         break;
  206.         } 
  207.         sprintf(buffer, "%s/%s.lproj/%s.strings", launchDir, *languages, tableName);
  208.         table =  [[NXStringTable alloc] init] ;
  209.         [table readFromFile:buffer];
  210.         languages++;
  211.     }
  212.     if (!table) {
  213.         if (debugLanguages && !isMyAppsLanguage) NXLogError("Cannot parse '%s' strings.", tableName);
  214.         table = [NXStringTable new];
  215.     }
  216.     if (!stringTableTable) stringTableTable = [HashTable newKeyDesc:"*"];
  217.     [stringTableTable insertKey:tableName value:table];
  218.     }
  219.  
  220.     tableValue = [table valueForStringKey:key];
  221.  
  222.     if (!tableValue) {
  223.     if (debugLanguages && !isMyAppsLanguage)
  224.         NXLogError("Cannot find value for string '%s' in table '%s'.", key, tableName);
  225.     tableValue = value ? value : key;
  226.     [table insertKey:key value:(void *)value];
  227.     }
  228. /*
  229.     NXHashState state;
  230.     const char *dkey;const char *dvalue;
  231.  
  232.     state=[table initState];
  233.         while([table nextState:(NXHashState *)&state key:(const void **)&dkey value:(void **)&dvalue] == YES)
  234.             printf("key = %s, value = %s\n",dkey,dvalue);
  235. */
  236.  
  237.     return tableValue;
  238. }
  239.